package cn.invest.merry.module.invest.service.zbbase;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.invest.merry.module.invest.controller.admin.zbbase.vo.*;
import cn.invest.merry.module.invest.dal.dataobject.zbbase.ZbBaseDO;
import cn.invest.merry.framework.common.pojo.PageResult;
import cn.invest.merry.framework.common.pojo.PageParam;
import cn.invest.merry.framework.common.util.object.BeanUtils;

import cn.invest.merry.module.invest.dal.mysql.zbbase.ZbBaseMapper;

import static cn.invest.merry.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.invest.merry.module.invest.enums.ErrorCodeConstants.*;

/**
 * 个股报告期指标 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class ZbBaseServiceImpl implements ZbBaseService {

    @Resource
    private ZbBaseMapper zbBaseMapper;

    @Override
    public Long createZbBase(ZbBaseSaveReqVO createReqVO) {
        // 插入
        ZbBaseDO zbBase = BeanUtils.toBean(createReqVO, ZbBaseDO.class);
        zbBaseMapper.insert(zbBase);
        // 返回
        return zbBase.getId();
    }

    @Override
    public void updateZbBase(ZbBaseSaveReqVO updateReqVO) {
        // 校验存在
        validateZbBaseExists(updateReqVO.getId());
        // 更新
        ZbBaseDO updateObj = BeanUtils.toBean(updateReqVO, ZbBaseDO.class);
        zbBaseMapper.updateById(updateObj);
    }

    @Override
    public void deleteZbBase(Long id) {
        // 校验存在
        validateZbBaseExists(id);
        // 删除
        zbBaseMapper.deleteById(id);
    }

    private void validateZbBaseExists(Long id) {
        if (zbBaseMapper.selectById(id) == null) {
            throw exception(ZB_BASE_NOT_EXISTS);
        }
    }

    @Override
    public ZbBaseDO getZbBase(Long id) {
        return zbBaseMapper.selectById(id);
    }

    @Override
    public PageResult<ZbBaseDO> getZbBasePage(ZbBasePageReqVO pageReqVO) {
        return zbBaseMapper.selectPage(pageReqVO);
    }

}